﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RESP_IIWaterAssessment
{
    public partial class EditCanalForm : Form
    {
        tb_canal canal;
        tb_CanalState canalState;
        int avpID;

        public EditCanalForm(tb_canal canal, int avpID)
        {
            InitializeComponent();
            this.canal = canal;
            this.avpID = avpID;
            this.ShowInTaskbar = false;
        }

        public tb_canal Canal
        {
            get
            {
                return canal;
            }
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(tbCanal.Text))
            {
                if (InformationClass.Lang)
                    MessageBox.Show("Не введено имя канала");
                else
                    MessageBox.Show("Канал номи киритилмаган");
                return;
            }

            int WaterUsers;
            decimal length, area;
            if (!Int32.TryParse(tbWaterUser.Text, out WaterUsers) || !tbLength.Text.GetDecimalOrDefault(out length) || !tbArea.Text.GetDecimalOrDefault(out area))
            {
                if (InformationClass.Lang)
                    MessageBox.Show("Не верно заполненны данные");
                else
                    MessageBox.Show("Маълумотлар нотўғри киритилган");
                return;
            }

            if (canal != null)
            {
                canal.CoverType = cbCoverType.SelectedIndex >= 0 ? Convert.ToInt32(cbCoverType.SelectedValue) : 0;
                canal.AVPID = avpID;
                canal.Area = area != null ? area : 0;
                canal.Length = length != null ? length : 0;
                canal.WaterUser = WaterUsers != null ? WaterUsers : 0;
                canal.Note = tbNote.Text;
            }
            else
            {
                if (DBClass.Canals.Where(wh => wh.AVPID == avpID && wh.Name == tbCanal.Text).Select(se => se.Name).ToArray().Contains(tbCanal.Text))
                {
                    MessageBox.Show(msgClass.msgUncorrectData, msgClass.msgAchtung);
                    tbCanal.Focus();
                    tbCanal.SelectionStart = 0;
                    tbCanal.SelectionLength = tbCanal.Text.Length;
                    return;
                }
                canal = new tb_canal
                {
                    Area = area != 0 ? area : 0,
                    AVPID = avpID,
                    CoverType = cbCoverType.SelectedIndex >= 0 ? Convert.ToInt32(cbCoverType.SelectedValue) : 0,
                    Length = length != 0 ? length : 0,
                    Name = tbCanal.Text,
                    Note = tbNote.Text,
                    WaterUser = WaterUsers != 0 ? WaterUsers : 0
                };
            }
            DialogResult = DialogResult.OK;
            this.Close();
        }

        private void EditCanalForm_Load(object sender, EventArgs e)
        {
            cbCoverType.DataSource = UtilClass.FillCBCategory("ChannelType");
            cbCoverType.DisplayMember = "Name";
            cbCoverType.ValueMember = "ID";
            cbCoverType.SelectedIndex = -1;
            cbCoverType.Text = string.Empty;
            cbCoverType.Enabled = true;

            if (canal != null)
            {
                tbCanal.Text = canal.Name;
                tbCanal.Enabled = false;
                tbLength.Text = canal.Length.Value.ToString();
                tbArea.Text = canal.Area.Value.ToString();
                tbWaterUser.Text = canal.WaterUser.Value.ToString();
                tbNote.Text = canal.Note;
                cbCoverType.SelectedIndex = cbCoverType.FindStringExact(UtilClass.GetCategoryDisplayName((int)canal.CoverType, "ChannelType"));
            }
        }
    }
}
